﻿using Janus.BaseEntitys;
using Janus.Cores.Extension;
using Janus.Cores.Linq;
using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using Janus.Mes.VO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Janus.Mes.WebClient.Areas.System.Controllers
{
    public class SysConfigController : BaseController
    {


        #region 构造
        public SysConfigController()
        {
        }
        #endregion

        #region 列表
        /// <summary>
        /// 系统参数设置首页（返回数据列表）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            //return View(GetList(new FormCollection()));
            return View(new List<SysConfigVO>());
        }

        /// <summary>
        /// 返回查询数据
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(GetList(collection));
        }

        #endregion

        #region 新增修改
        [HttpGet]
        public ActionResult Create(string id)
        {
            SysConfigVO sysConfig = null;
            ViewBag.IsCreate = true;
            if (!string.IsNullOrEmpty(id))
            {
                ViewBag.IsCreate = false;
                Expression<Func<SysConfigDTO, bool>> predicate = (c) => c.Key == id;
                var model = SysConfigServices.FindOne(predicate.ToWhere());
                sysConfig = AutoMapper.Mapper.DynamicMap<SysConfigVO>(model);
            }
            else
            {
                sysConfig = base.InitModel<SysConfigVO>(sysConfig);
            }
            return View(sysConfig);
        }

        /// <summary>
        /// 新增系统参数
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Create(FormCollection form)
        {
            try
            {
                var result = false;
                SysConfigVO model = new SysConfigVO();
                model = base.SetModel<SysConfigVO>(form);
                var sysConfig = AutoMapper.Mapper.DynamicMap<SysConfigDTO>(model);
                result = SysConfigServices.Save(sysConfig) > 0;
                if (result)
                {
                    ajaxStr = base.AjaxResultSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 主键是否存在
        [HttpPost]
        public string IsExist()
        {
            string data = Request["data"];
            Expression<Func<SysConfigDTO, bool>> predicate = (c) => c.Key == data;
            bool result = SysConfigServices.IsExists(predicate.ToWhere());
            if (!result)
            {
                ajaxStr = base.AjaxResultSucess;
            }
            else
            {
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除系统参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Delete(object id)
        {
            try
            {
                var result = SysConfigServices.Delete(id);
                if (result)
                {
                    ajaxStr = base.AjaxDeleteSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }

            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 私有方法
        protected IEnumerable<SysConfigVO> GetList(FormCollection collection)
        {
            var searchParma = base.SearchPara<SysConfigDTO>(collection);
            if (searchParma.Model.Title.IsNotEmpty())
            {
                searchParma.Queriers.Where("Title.Contains(@0)", searchParma.Model.Title);
            }
            //可以继续判断 增加查询条件

            var result = base.FindAll<SysConfigDTO>(searchParma, SysConfigServices);
            if (!result.ErrorMessage.IsNotEmpty())
            {
                var list = AutoMapper.Mapper.DynamicMap<List<SysConfigVO>>(result.Result);
                return list;
            }
            else
            {
                return new List<SysConfigVO>();
            }
        }
        #endregion

    }

}